<?php
//------- logistic wxapp -------

namespace api\logi\controller;

use api\user\model\UserFavoriteModel;
use api\user\model\UserLikeModel;
use cmf\controller\RestBaseController;
use api\logi\model\LogiPostModel;
use think\Db;

class LogiController extends RestBaseController {
    protected $postModel;

    public function __construct(LogiPostModel $postModel) {
        parent::__construct();
        $this->postModel = $postModel;
    }

    /**
     * 订单列表
     */
    public function getList() {
        $params = $this->request->get();
        // $params['where']['post_type'] = 1;
        $data = $this->postModel->getDatas($params);

        if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
            $response = $data;
        } else {
            $response = ['list' => $data,];
        }
        $this->success('请求成功!', $response);
    }

    /**
     * 获取指定的订单
     * @param int $id
     */
    public function read($id) {
        if (intval($id) === 0) {
            $this->error('无效的订单id！');
        } else {
            $params                       = $this->request->get();
            $params['id']                 = $id;
            $data                         = $this->postModel->getDatas($params);
            if (empty($data)) {
                $this->error('订单不存在！');
            } else {
                $data = $data->toArray();
                $this->success('请求成功!', $data);
            }

        }
    }

    /**
     * 我的订单列表
     */
    public function my() {
        $params = $this->request->get();
        $userId = $this->getUserId();
        $data   = $this->postModel->getUserLogi($userId, $params);

        if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
            $response = [$data];
        } else {
            $response = ['list' => $data];
        }

        $this->success('请求成功!', $response);
    }

    /**
     * 删除订单
     * @param  int $id
     */
    public function delete($id) {
        if (empty($id)) {
            $this->error('无效的订单id');
        }
        $result = $this->postModel->deleteLogi($id, $this->getUserId());
        if ($result == -1) {
            $this->error('订单已删除');
        }
        if ($result) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

}